package com.ruanxie.json.deserializer;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;

/**
 * xnm(学年名)的反序列化器：
 * 值：
 *      "3":表示上学期，转化为"1"
 *      "12":表示下学期，转化为"2"
 */
public class TermDeserializer implements ObjectDeserializer {
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        String originalTerm = parser.parseObject(String.class);
        return (T) convertTerm(originalTerm);
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }

    private String convertTerm(String originalTerm) {
        switch (originalTerm) {
            case "12":
                return "2";
            case "3":
                return "1";
            default:
                return originalTerm;
        }
    }
}
